﻿//function ParseXML(xmlPath, xslPath) {
//    var xml;
//    var xsl;
//    var htmlString;     
//    try {
//        xml = new ActiveXObject("Microsoft.XMLDOM");
//        xsl = new ActiveXObject("Microsoft.XMLDOM");
//    }
//    catch (e) {
//        try {
//            xml = document.implementation.createDocument("", "", null);
//            xsl = document.implementation.createDocument("", "", null);
//        }
//        catch (e) {
//            alert("数据解析失败！" + e.message);
//        }
//    }

//    // Load XML
//    xml.async = false;
//    xml.load(xmlPath);

//    // Load XSL
//    xsl.async = false;
//    xsl.load(xslPath);

//    // Transform
//    try {
//        htmlString = xml.transformNode(xsl);
//    }
//    catch (e) {
//        try {
//            var xsltProcessor = new XSLTProcessor();
//            xsltProcessor.importStylesheet(xsl);

//            var result = xsltProcessor.transformToFragment(xml, document);
//            var xmls = new XMLSerializer();
//            htmlString = xmls.serializeToString(result);
//        }
//        catch (e) {
//            alert("数据解析失败！" + e.message);
//        }
//    }

//    return htmlString;
//}



function ParseXML(xmlPath, xslPath) {
    var xml;
    var xsl;
    var htmlString;

    try {
        xml = new ActiveXObject("Microsoft.XMLDOM");
        xsl = new ActiveXObject("Microsoft.XMLDOM");

        // Load XML
        xml.async = false;
        xml.load(xmlPath);

        // Load XSL
        xsl.async = false;
        xsl.load(xslPath);

        // Transform
        htmlString = xml.transformNode(xsl);
    }
    catch (e) {
        try {
            xml = new XMLHttpRequest();
            xml.open("GET", xmlPath, false);
            xml.send(null);
            var xmlDoc = (new DOMParser()).parseFromString(xml.responseText, "text/xml");
            
            xsl = new XMLHttpRequest();
            xsl.open("GET", xslPath, false);
            xsl.send(null);
            var xslt = xsl.responseXML;
            
            var xsltProcessor = new XSLTProcessor();
            xsltProcessor.importStylesheet(xslt);

            var result = xsltProcessor.transformToFragment(xmlDoc, document);
            var xmls = new XMLSerializer();
            htmlString = xmls.serializeToString(result);
        }
        catch (e) {
            alert("数据解析失败！" + e.message);
        }
    }

    return htmlString;
//    try {
//        xml = new XMLHttpRequest();
//        xml.open("GET", xmlPath, false);
//        xml.send(null);
//        var xmlDoc = (new DOMParser()).parseFromString(xml.responseXML, "text/xml");
//        xsl = new XMLHttpRequest();
//        xsl.open("POST", xslPath, false);
//        xsl.send(null);
//        var xslt = xsl.responseXML;

//        var xsltProcessor = new XSLTProcessor();
//        xsltProcessor.importStylesheet(xslt);

//        var result = xsltProcessor.transformToFragment(xmlDoc, document);
//        var xmls = new XMLSerializer();
//        htmlString = serializeToString(result);
//    }
//    catch (e) {
//    }
}